//
//  TradeOrderStyleOneCell.swift
//  Exchange
//
//  Created by rui on 2018/4/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class TradeOrderStyleOneCell: UITableViewCell {

    @IBOutlet weak var icon: UIButton!
    @IBOutlet weak var state: UILabel!
    @IBOutlet weak var varity: UILabel!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var redView: UIView!
    var model: WaitOrderModel? {
        didSet {
            self.configer()
        }
    }
    
    typealias IconBlock = (_ userId: Int)->()
    
    var iconC: IconBlock?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.icon.layer.cornerRadius = 22
        self.icon.layer.masksToBounds = true
        self.varity.layer.cornerRadius = 3
        self.varity.layer.borderColor = kThemeColor.cgColor
        self.varity.layer.borderWidth = 1
        self.varity.layer.masksToBounds = true
        self.redView.layer.cornerRadius = 5
        self.redView.isHidden = true
        let view = UIView(frame: self.bounds)
        view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        self.selectedBackgroundView = UIView(frame: self.bounds)
        self.selectedBackgroundView?.themeBackground = ThemeManager.ThemeColorKeys.selectTabColor
    }
    
    private func configer(){
        guard let model = self.model else {
            return
        }
        if let str = model.portrait,let url = URL(string: str) {
            self.icon.sd_setImage(with: url, for:.normal, placeholderImage: #imageLiteral(resourceName: "login_icon"))
        }else {
            self.icon.setImage(#imageLiteral(resourceName: "login_icon"), for: .normal)
        }
        self.name.text = model.userName
        var moneyType = model.moneyType ?? ""
        
        var isBuyer = false
        if model.buyer == AppInfo.default.userId {
           isBuyer = true
        }
        if AppCacheInfo.default.isEnlish {
            moneyType = " " + moneyType
        }
        if isBuyer {
            self.varity.text = kText("购买")+"\(moneyType)"
        }else {
            self.varity.text = kText("出售") + "\(moneyType)"
        }
        
        self.state.text = kText("待下单")
        if let isNewMessage  =  model.isNewMessage {
            self.redView.isHidden = !isNewMessage
        }else {
            self.redView.isHidden = true
        }
    }
    
    @IBAction func iconClick(_ sender: Any) {
        if let userId = model?.userId {
            iconC?(userId)
        }else {
            CYMBShow.showMessage("用户不存在")
        }
    }
}
